package cn.kgc.controller;

import cn.kgc.pojo.GoodsDetail;
import cn.kgc.pojo.GoodsSort;
import cn.kgc.service.GoodsDetailService;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class GoodsDetailController {
    @Resource
    private GoodsDetailService goodsDetailService;

    @RequestMapping(value = "/queryAllDetail")
    @ResponseBody
    public String queryAllSort(Integer sortId){
        List<GoodsDetail> list = goodsDetailService.queryAllDetail(sortId);
        return JSON.toJSONString(list);
    }
    @RequestMapping(value = "/shopGoods")
    public String shopGoods(Integer id, HttpServletRequest request){

        GoodsDetail goodsDetail=goodsDetailService.shopGoodsDetailById(id);

        request.setAttribute("goodsDetail",goodsDetail);
        return "forward:shopGoods.jsp";
    }
    @RequestMapping(value = "/buy")
    public String buy(Integer id, Integer buyNum, Model model){
        int i = goodsDetailService.buy(id,buyNum);
        if (i>0){
            model.addAttribute("message","购买成功！");
            return "index";
        }else {
            return "shopGoods";
        }
    }
}
